package com.maiji.cloud.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class UUID_MD5 {

    public static String getUUID () {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String randomPwd () {
            String[] pa1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
            String[] pa2 = { "A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L",
                    "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 6; i++) {
                sb.append(pa1[(Double.valueOf(Math.random() * pa1.length).intValue())]);
                sb.append(pa2[(Double.valueOf(Math.random() * pa2.length).intValue())]);
            }
            return sb.toString();
    }

    public static String randomNickName () {
            String[] pa1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
            String[] pa2 = { "A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L",
                    "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            StringBuffer sb = new StringBuffer("麦吉-");
            for (int i = 0; i < 6; i++) {
                if (i <=1) sb.append(pa2[(Double.valueOf(Math.random() * pa2.length).intValue())]);
                else sb.append(pa1[(Double.valueOf(Math.random() * pa1.length).intValue())]);
            }
            return sb.toString();
    }

    public static String md5Str(String str) {
        try {
            // 得到一个信息摘要器
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(str.getBytes());
            StringBuffer buffer = new StringBuffer();
            // 把每一个byte 做一个与运算 0xabcd;
            for (byte b : result) {
                // 与运算
                int number = b & 0xabcd;// 加盐
                String strProcessed = Integer.toHexString(number);
                if (str.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(strProcessed);
            }
            // 标准的md5加密后的结果
            return buffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String orderNo() {
        Long currentTimeMillis = System.currentTimeMillis();
        String[] split = currentTimeMillis.toString().split("");
        StringBuffer sb = new StringBuffer();
        for (int i=0; i<split.length; i++) {
            if (i<(20-split.length)) split[i] += (int) (Math.random() * 10);
            sb.append(split[i]);
        }
        return sb.toString();
    }

    public static void main (String[] args) {
//        String str = randomPwd();
//        System.out.println(str);
//        System.out.println("4M5C4p".matches("^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z\\d]{6,}$"));
       System.out.println(md5Str("123456"));
    }

}
